package com.smatiger.hotdeploy.loader.listener;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.smatiger.hotdeploy.loader.application.ApplicationManager;
import com.smatiger.hotdeploy.loader.constants.Constants;

/**
* @Description: jar 文件改变监听器
* @author smatiger
* @date 2017年6月30日 下午5:33:57 
*/
public class JarFileChangeListener implements FileListener {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void fileChanged(FileChangeEvent arg0) throws Exception {
        FileObject file = arg0.getFile();
        if (file == null) {
            return;
        }
        String fileName = file.getName().getBaseName();
        logger.info("file {} changed!", fileName);
        if (StringUtils.isBlank(fileName)
            || !fileName.toUpperCase().endsWith(Constants.JAR_FILE_SUFFIX)) {
            return;
        }
        ApplicationManager.getInstance().reload(fileName);
    }

    @Override
    public void fileCreated(FileChangeEvent arg0) throws Exception {
        logger.info("file[{}] created!", arg0.getFile().getURL().toString());
    }

    @Override
    public void fileDeleted(FileChangeEvent arg0) throws Exception {
        logger.info("file[{}] deleted!", arg0.getFile().getURL().toString());
    }

}
